{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "eef6934f",
   "metadata": {},
   "source": [
    "# python基础入门7_  Iterables vs Iterators\n",
    "\n",
    "在Python编程中，Iterable和Iterator是两个非常重要的概念。可迭代对象（Iterable）是指可以使用for循环遍历的对象，例如列表、元组、字典等，它们遵循可迭代的协议，即包含__iter__()方法。而迭代器（Iterator）则是实现了迭代器协议的对象，具体来说，只要一个对象定义了next()（Python 2版本）或者__next__()（Python 3版本）方法，那么它就是一个迭代器。迭代器中的__iter__()方法应返回其自身，因此迭代器也是一种可迭代对象。\n",
    "\n",
    "更具体地说，当我们使用for循环来遍历一个可迭代对象时，Python会自动将该可迭代对象转换为一个迭代器。在这个过程中，for循环首先调用该可迭代对象的__iter__()方法来获取该对象的迭代器，然后通过调用迭代器的next()或__next__()方法来获取序列中的下一个元素。当没有更多的元素可以访问时，next()或__next__()方法会抛出StopIteration异常，从而结束遍历。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ac070f1",
   "metadata": {},
   "source": [
    "## Iterable\n",
    "\n",
    "可迭代对象（Iterable）是指可以使用for循环遍历的对象，例如列表、元组、字典等，它们遵循可迭代的协议，即包含iter()方法。iter(iterable) -> iterator。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "329e4e56",
   "metadata": {},
   "outputs": [],
   "source": [
    "lst=[1,2,3,4,5]#Iterable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "8a953107",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst\n",
    "#If no argument（参数） is given, the constructor(构造函数) creates a new empty list.\n",
    "#The argument must be an iterable if specified."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "dd5a2b2d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "for i in lst:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "055a7d3c",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'list' object is not an iterator",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_12340/615357584.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlst\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: 'list' object is not an iterator"
     ]
    }
   ],
   "source": [
    "next(lst)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95e51cfb",
   "metadata": {},
   "source": [
    "## Iterator\n",
    "\n",
    "迭代器（Iterator）则是实现了迭代器协议的对象，具体来说，只要一个对象定义了next()（Python 2版本）或者next()（Python 3版本）方法，那么它就是一个迭代器。迭代器中的iter()方法应返回其自身，因此迭代器也是一种可迭代对象。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "a92d33d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "lst = iter(lst)#iter(iterable) -> iterator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "8b352b1b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "58dc1192",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "5dd421ee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "for i in lst:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6d015df2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "f14695c8",
   "metadata": {},
   "source": [
    "### 判断对错：\n",
    "在\n",
    "for i in lst:\n",
    "    print(i)\n",
    "代码中，i就是iterator，lst就是iterable。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "deed66eb",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'int' object is not an iterator",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_12340/68269482.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mlst\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m     \u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: 'int' object is not an iterator"
     ]
    }
   ],
   "source": [
    "for i in lst:\n",
    "    next(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "86d0cf5d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
